package com.erdos.cas.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.erdos.cas.client.MesHttpClient;
import com.erdos.cas.entity.ComboBoxEntity;

public class Login extends JFrame implements ActionListener {
		
	private JLabel[] label;
		
	private JTextField name;
		
	private JPasswordField password;
		
	private JButton ok;
		
	private JButton cancel;
		
	private JPanel jp1, jp2, jp3,jp4;
	
	//班次
	private JComboBox shiftNr;
	
	//班时
	private JComboBox shiftTime;
	
	//班时
	private JComboBox equipment;

	public Login() {
		super("单点登录");
		label = new JLabel[5];
		label[0] = new JLabel("用户名:",SwingConstants.RIGHT);
		label[1] = new JLabel("   密码:",SwingConstants.RIGHT);
		label[2] = new JLabel("   班次:",SwingConstants.RIGHT);
		label[3] = new JLabel("   班时:",SwingConstants.RIGHT);
		label[4] = new JLabel("   设备:",SwingConstants.RIGHT);		
		
		name = new JTextField(15);
		password = new JPasswordField(15);
		ok = new JButton("登录");
		cancel = new JButton("取消");
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		jp4.setVisible(true);
		
		Dimension size=new Dimension(250,30);
		//初始化班次
		shiftNr = new JComboBox();
		shiftNr.setPreferredSize(size);
		
		ComboBoxEntity[] shiftNrValue = new ComboBoxEntity[4];		
		shiftNrValue[0] = new ComboBoxEntity("甲","1");
		shiftNrValue[1] = new ComboBoxEntity("乙","2");
		shiftNrValue[2] = new ComboBoxEntity("丙","3");
		shiftNrValue[3] = new ComboBoxEntity("丁","4");
		for (int i = 0; i < shiftNrValue.length; i++) {
		    shiftNr.addItem(shiftNrValue[i]);
		}		
		
		//初始化班时
		shiftTime = new JComboBox();
		shiftTime.setPreferredSize(size);
		
		ComboBoxEntity[] shiftTimeValue = new ComboBoxEntity[3];		
		shiftTimeValue[0] = new ComboBoxEntity("0-8","1");
		shiftTimeValue[1] = new ComboBoxEntity("8-16","2");
		shiftTimeValue[2] = new ComboBoxEntity("16-24","3");		
		for (int i = 0; i < shiftTimeValue.length; i++) {
			shiftTime.addItem(shiftTimeValue[i]);
		}
		
		//初始化设备
		equipment = new JComboBox();
		equipment.setPreferredSize(size);
		
		ComboBoxEntity[] equipmentValue = new ComboBoxEntity[12];
		equipmentValue[0] = new ComboBoxEntity("1#转炉","BOF1");
		equipmentValue[1] = new ComboBoxEntity("2#转炉","BOF2");
		equipmentValue[2] = new ComboBoxEntity("3#转炉","BOF3");
		equipmentValue[3] = new ComboBoxEntity("CAS炉","CAS");
		equipmentValue[4] = new ComboBoxEntity("AS炉","AS");
		equipmentValue[5] = new ComboBoxEntity("LF炉","LF");
		equipmentValue[6] = new ComboBoxEntity("1#RH炉","RH");
		equipmentValue[7] = new ComboBoxEntity("2#RH炉","SRH");
		equipmentValue[8] = new ComboBoxEntity("1#板坯","SCM1");
		equipmentValue[9] = new ComboBoxEntity("2#板坯","SCM2");	
		equipmentValue[10] = new ComboBoxEntity("1#方坯","BCM1");
		equipmentValue[11] = new ComboBoxEntity("2#方坯","BCM2");
		for (int i = 0; i < equipmentValue.length; i++) {
		    equipment.addItem(equipmentValue[i]);
		}
		
		
		init();
		setAllFont();
		addEventHandle();
		keyHandler listener = new keyHandler();
		password.addKeyListener(listener);
		name.addKeyListener(listener);
		ok.addKeyListener(listener);
	}

	/**
	 * 界面布局的初始化
	 */
	private void init() {
	        	
		jp1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
		//jp1.add(label[2]);
		getContentPane().add(jp1, BorderLayout.NORTH);
		
		jp2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
		jp2.add(label[0]);		
		jp2.add(name);
		jp2.add(label[1]);
		jp2.add(password);		
		jp2.add(label[2]);
		jp2.add(shiftNr);		
		jp2.add(label[3]);
		jp2.add(shiftTime);		
		jp2.add(label[4]);
		jp2.add(equipment);	
		getContentPane().add(jp2, BorderLayout.CENTER);		
		
		jp3.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));
		jp3.add(ok);
		jp3.add(cancel);
		getContentPane().add(jp3, BorderLayout.SOUTH);
	}

	/**
	 * 设置组件与文字的字体
	 */
	private void setAllFont() {
		Font f1 = new Font("", Font.BOLD, 20);
		Font f2 = new Font("仿宋",Font.BOLD,30);
		for (int i = 0; i < label.length; i++) {
			label[i].setFont(f1);
		}
		//label[label.length - 1].setFont(f2);
		//label[label.length - 1].setForeground(Color.RED);
		name.setFont(f1);
		password.setFont(f1);
		shiftNr.setFont(f1);
		shiftTime.setFont(f1);
		equipment.setFont(f1);
		ok.setFont(f1);
		cancel.setFont(f1);
	}

	/**
	 * 用于显示界面
	 */
	public void showMe() {
		this.setBounds(300, 200, 400, 350);
		Rectangle bounds = new Rectangle();
		bounds.setBounds(300, 200, 400, 350);
		this.setMaximizedBounds(bounds);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}

	/**
	 * 关闭窗口的事件监听方法
	 * 
	 * @author Administrator
	 * @since  2008-7-30
	 */
	private void addEventHandle() {
		ok.addActionListener(this);
		cancel.addActionListener(this);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				int op = JOptionPane.showConfirmDialog(new JFrame(),"你确定要退出程序吗?", "",JOptionPane.YES_NO_CANCEL_OPTION);
				if (op == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}
		});
	}

	/**
	 * 按钮的事件监听方法
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("登录")) {
			if (name.getText().equals("")
					|| new String(password.getPassword()).equals("")) {
				JOptionPane.showMessageDialog(this, "用户名和密码不能为空","",JOptionPane.YES_NO_OPTION);
				return;
			} else {
				tologin();
			}
		} else if (e.getActionCommand().equals("取消")) {
			try {
				name.setText("");
				password.setText("");
				this.showMe();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new Login().showMe();
			
	}
	
	/**
	 * 获得用户名和密码后调用此方法
	 */
	private void tologin(){	        
	        
		String userName = name.getText();
		String pwd = new String(password.getPassword());
		
		System.out.println(pwd);
		
		ComboBoxEntity shiftNrValue=(ComboBoxEntity)shiftNr.getSelectedItem();
		System.out.println(shiftNrValue.getValue());
		
		ComboBoxEntity shiftTimeValue=(ComboBoxEntity)shiftTime.getSelectedItem();
		System.out.println(shiftTimeValue.getValue());
		
		ComboBoxEntity equipmentValue=(ComboBoxEntity)equipment.getSelectedItem();
		System.out.println(equipmentValue.getValue());
		
		MesHttpClient httpClient = new MesHttpClient();
		List appList = httpClient.checkLogin(userName, pwd);
		if(appList.get(0).equals("noUser")){
			JOptionPane.showMessageDialog(new JFrame(), "用户名或密码错误");
			name.setText("");
			password.setText("");
			this.showMe();
		}else if(appList.get(0).equals("noAppList")){
			JOptionPane.showMessageDialog(new JFrame(), "您没有可访问列表");
			name.setText("");
			password.setText("");
			this.showMe();
		}else if(appList.get(0).equals("logined")){
		    JOptionPane.showMessageDialog(new JFrame(), "您已经在别的地方登录了。");
		    name.setText("");
		    password.setText("");
		    this.showMe();
		}else{	
		    
			new AppListFrame(appList);
			this.dispose();
		}
	}
	
	/**
	 * ENTER键的监听器 
	 */
	private class keyHandler implements KeyListener{

		/* (non-Javadoc)
		 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
		 */
		public void keyPressed(KeyEvent e) {
			int keyCode = e.getKeyCode();
			if(keyCode == KeyEvent.VK_ENTER){
				if (name.getText().equals("")
						|| new String(password.getPassword()).equals("")) {
					JOptionPane.showMessageDialog(new JFrame(), "用户名和密码不能为空","",JOptionPane.YES_NO_OPTION);
					return;
				}else{
					tologin();
				}
					
			}
		}

		/* (non-Javadoc)
		 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
		 */
		public void keyReleased(KeyEvent e) {
			
			
		}

		/* (non-Javadoc)
		 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
		 */
		public void keyTyped(KeyEvent e) {
			
			
		}
		
	}
}

